import { IGameSystem } from "../Interfaces/IGameSystem";
import { GameCore } from "./Class";
import { EventSystem } from "./EventSystem";
import { Constructor, getClassName, NewClass } from "./Global";


export class Singleton
{
    private static singletonMap : Map<string, GameCore.ClassBase> = new  Map<string, GameCore.ClassBase>();
    public static Instance<T extends GameCore.ClassBase>(obj : Constructor<T>) : T
    {
        var className = getClassName(obj);
        if(this.singletonMap.has(className))
        {
            return this.singletonMap.get(className) as T;
        }

        var instance = NewClass(obj);
        if(instance != null){
            this.singletonMap.set(className, instance);
        }

        var imp = IGameSystem.TryParseToAwkeSystem(instance);
        if(imp != null){
            EventSystem.Instance.ResigerAwakeEventList(imp);
        }
        
        var imp_update = IGameSystem.TryParseToUpdateSystem(instance);
        if(imp_update != null){
            EventSystem.Instance.RegeisterUpdateEvent(imp_update);
        }

        return instance;
    }
}


